<?php

namespace app\api\controller\user;

use app\api\controller\Controller;
use app\api\model\User as UserModel;
use app\api\model\Order as OrderModel;
use app\api\model\Setting as SettingModel;
use app\api\model\UserCoupon as UserCouponModel;
use app\common\model\dealer\CapitalNew as CapitalNewModel;
use app\common\model\UserCapital;
use app\common\model\UserGrade;
use app\common\model\Withdraw;

/**
 * 个人中心主页
 * Class Index
 * @package app\api\controller\user
 */
class Index extends Controller
{
    /**
     * 获取当前用户信息
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail()
    {
        // 当前用户信息
        $user = $this->getUser(false);
        // 订单总数
        $model = new OrderModel;
        $coupon_model = new UserCouponModel;
        $capital_model = new CapitalNewModel;
        //var_dump($user);die;
        if($user){
            $user['coupon'] = $coupon_model->getCount($user['user_id']);
            $user['commission_money'] = $capital_model->where('user_id',$user['user_id'])->where(['type'=>['NEQ',3]])->sum('money');
        }
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $total = $user['balance'];//UserCapital::where(['user_id'=>$user['user_id'],'type'=>['in','1,2']])->sum('money');//累计
        $today = UserCapital::where(['user_id'=>$user['user_id'],'type'=>['in','1,2'],'create_time'=>['between',"$beginToday,$endToday"]])->sum('money');//今日佣金
        $withdrawn = Withdraw::where(['user_id'=>$user['user_id'],'apply_status'=>['in','10']])->sum('money');//待提现佣金
        $withdrawn_d = Withdraw::where(['user_id'=>$user['user_id'],'apply_status'=>['in','10,20']])->sum('money');//待提现佣金
        $commission_amount = $total-$withdrawn_d;//佣金金额
        if($user['grade_id'] < 6){
            $user_grade = UserGrade::where(['grade_id'=>$user['grade_id']+1])->find();
            $upgrade = json_decode($user_grade['upgrade'],true);
            $grade['surplus'] = $upgrade['expend_money'] - $user['expend_money'];
            $grade['name'] = $user_grade['name'];
        }else{
            $grade['surplus'] = 0;
            $grade['name'] = '最高等级';
        }

        return $this->renderSuccess([
            'userInfo' => $user,
            'commission' => [
                'total'=>$total,//累计佣金
                'today'=>$today,//今日佣金
                'withdrawn'=>$withdrawn,//待提现佣金
                'commission_amount'=>$commission_amount,//佣金金额
                'grade' => $grade,
            ],
            'orderCount' => [
                'payment' => $model->getCount($user, 'payment'),
                'received' => $model->getCount($user, 'received'),
                'comment' => $model->getCount($user, 'comment'),
                'deliver' => $model->getCount($user, 'deliver'),
                'fter_sale' => $model->getCount($user, 'fter_sale'),
            ],
            'setting' => [
                'points_name' => SettingModel::getPointsName(),
            ],
            'menus' => (new UserModel)->getMenus()   // 个人中心菜单列表
        ]);
    }

}
